package com.lsm.sm.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.lsm.sm.util.Const;
import com.lsm.sm.util.WebUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class NewHttpSessionInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = -2388131493162838368L;
	
	@Override
	public String intercept(ActionInvocation invok ) throws Exception {
		boolean isNew = false;
		HttpServletRequest request = WebUtil.getHttpRequest();
		Map<String, Object> session = null;
		//查看 session 是否过期，如果过期则创建一个，并在session(map) 中放入一个标识
		if( request.getSession(false) == null ){
			request.getSession();
			isNew = true;
			session = WebUtil.getSession();
			session.put( Const.NEW_SESSION, 1 );
		}
		String result = null;
		try{
			result = invok.invoke();
		}catch( Exception e ){
			return "login";
		}
		
		//在 action 运行后再运行，将该标识 删除
		if( isNew ){
			session.remove( Const.NEW_SESSION );
			isNew = false;
		}
		return result;
	}

}
